import Vue from "vue";
import VueRouter from "vue-router";
import { routerMode } from "@/config/env";
import routes from "./routes";
Vue.use(VueRouter);

// 忽略规则
const ignore = {
	token: ["/login", "/403", "/404", "/500", "/502", "/socket"]
};

const createRouter = () =>
	new VueRouter({
		mode: routerMode || "history",
		base: process.env.BASE_URL,
		routes,
		scrollBehavior(to, from, savedPosition) {
			if (savedPosition) {
				return savedPosition;
			} else {
				return { x: 0, y: 0 };
			}
		}
	});

const router = createRouter();
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
	const newRouter = createRouter();
	router.matcher = newRouter.matcher; // reset router
}

router.beforeEach((to, from, next) => {
	// el参数储存
	const { platform, biz_id, group_id } = to.query
	/**
 	* platform 平台来源参数
 	* @param electron electron桌面应用进入
 	* @param dingding 钉钉应用进入
 	*/
	if (platform == 'electron' || platform == 'dingding') {
		sessionStorage.setItem('dingAutoLogin', to.fullPath)
		sessionStorage.setItem('platform', platform)
		sessionStorage.setItem('extQuery', JSON.stringify(to.query))
		// if (biz_id) {
		// 	sessionStorage.setItem('biz_id', biz_id)
		// }
		// if (group_id) {
		// 	sessionStorage.setItem('group_id', group_id)
		// }
		// if (company_id) {
		// 	sessionStorage.setItem('group_id', group_id)
		// }
	}
	next()
})
export { ignore, routes };
export default router;
